package tbao.net.searsh.thread;

import java.util.concurrent.*;

public class ThreadTest {
   public static   ExecutorService service=Executors.newFixedThreadPool(10);


    public static void mains(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main.....start....");

  /*      CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println("CompletableFuture-当前线程" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果" + i);
        }, service);*/


        /**
         * 方法完成后的感知
         */
        /*CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("CompletableFuture-当前线程" + Thread.currentThread().getId());
            int i = 10 / 0;
            return i;
        }, service).whenComplete((res,exception)->{
            System.out.println("异步任务完成成功.....结果是:"+res+"异常是:"+exception);
        }).exceptionally(throwable -> {
            return 10;
        });
        Integer integer = integerCompletableFuture.get();*/

        /**
         * 方法执行完后处理
         */
    /*    CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("CompletableFuture-当前线程" + Thread.currentThread().getId());
            int i = 10 / 4;
            return i;
        }, service).handle((res,thr)->{
            if(res!=null){
                return res*2;
            }
            if(thr!=null){
                return 0;
            }
            return 0;
        });*/

        /**
         * 线程串行化
         * 不能获取上一步获取结果
         */
       /* CompletableFuture<Void> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("CompletableFuture-当前线程" + Thread.currentThread().getId());
            int i = 10 / 4;
            System.out.println("运行结果"+i);
            return i;
        }, service).thenRunAsync(()->{
            System.out.println("任务2启动");
        },service)*/;


 /*       CompletableFuture<String> stringCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println("CompletableFuture-当前线程" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果" + i);
        }, service).thenApplyAsync(res -> {
            System.out.println("任务2启动" + res);
            return "hello";
        }, service);
         System.out.println("main.....end....");*/



    }




    public static void thread(String[] args) throws ExecutionException, InterruptedException {

        System.out.println("main.....start....");

        /**
         * 1.继承Thread
         *         Thread01 thread01 = new Thread01();
         *         thread01.start();
         *
         * 2.实现Runnable 接口
         *         Runable01 runable01=new Runable01();
         *         new Thread(runable01).start();
         *
         * 3.实现Callable 接口 +FutureTask (阻塞等待)
         *         FutureTask<Integer> integerFutureTask = new FutureTask<Integer>(new Callable01());
         *         new Thread(integerFutureTask).start();
         *         Integer integer = integerFutureTask.get();
         *
         * 4.线程池
         * 七大参数
         * 1：corePoolSize :核心线程数
         * 2:maximumPoolSize:最大线程数
         * 3:keepAliveTime:存活时间（大于核心线程才释放空闲线程
         * 4:TimeUnit:时间单位
         * 5:BlockingQueue<Runnable>:阻塞队列
         * 6:threadFactory:线程的创建工程
         * 7: RejectedExecutionHandler handler:如果队列满了，按照指定的拒绝策略拒绝执行任务
         */

        TimeUnit unit;
        BlockingQueue workQueue;
        ThreadPoolExecutor executor=new ThreadPoolExecutor(5,
                200 ,
                10 ,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(100000),Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        /**
         *常见的 4 种线程池
         *1: Executors.newCachedThreadPool() 核心线程是0，所有都可以回收
         * 2:newFixedThreadPool() 固定大小 不可回收
         * 3:newSingleThreadScheduledExecutor() 定时任务线程池
         * 4:.newSingleThreadExecutor() 单线程的线程池,后台从队列里边获取任务挨个执行
         */
        ExecutorService service = Executors.newCachedThreadPool();

        System.out.println("main.....end....");
    }

    /**
     * 方式一 继承 Thread
     */
    public static class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("方式①:继承:Thread-当前线程"+Thread.currentThread().getId());
            int i=10/2;
            System.out.println("运行结果"+i);
        }
    }

    /**
     * 方式二 实现Runadble
     */
    public static class Runable01 implements Runnable{

        @Override
        public void run() {
            System.out.println("方式②:实现:Runable-当前线程"+Thread.currentThread().getId());
            int i=10/2;
            System.out.println("运行结果"+i);
        }
    }

    /**
     * 方式三 实现 Runnable 接口
     */
    public static class Callable01 implements Callable<Integer>{
        @Override
        public Integer call() throws Exception {
            System.out.println("方式③:实现 Runnable -当前线程"+Thread.currentThread().getId());
            int i=10/2;
            System.out.println("运行结果"+i);
            return i;
        }
    }

}
